import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import sys


class MainWindow(tk.Toplevel):

    sendLabelFrame_height = 150
    sendLabelFrame_width = 150
    recvLabelFrame_height = 150
    recvLabelFrame_width = 150

    def __init__(self, ROOTER=None) -> None:
        super().__init__()
        self.geometry("1000x600+200+200")
        self.title('uds手动界面')
        self.ROOTER = ROOTER

        self.tabControlInit()

        self.set_rooter_func_lib_dict()

        self.protocol('WM_DELETE_WINDOW', self.closeHandle)

    def set_rooter_func_lib_dict(self):
        # 设置测试函数库字典

        self.func_lib_dict = {
            'can_can': self.can_can_Tab,
            'uds_uds': self.uds_uds_Tab,
            'uds_can': self.uds_can_Tab,
            'ecu_unit': self.ecu_unit_Tab,
            'config_lib': self.config_lib
        }
        self.ROOTER.FUNC_LIB_DICT = self.func_lib_dict

    def closeHandle(self):
        # messagebox.showwarning('关闭窗口', '您将关闭窗口self...')
        res = messagebox.askyesno('退出窗口', 'yes:关闭 || no:隐藏')
        # print(f'[file:{__file__}]=>[line:{
        #   (sys._getframe().f_lineno)}]==>res:{res}')

        if res == True:
            self.destroy()
        else:
            self.withdraw()

    def tabControlInit(self):
        self.tabcontrol = ttk.Notebook(self)
        self.tabcontrol.grid(row=1, columnspan=5, sticky=tk.NW)

        # uds 手动操作
        # self.uds_manualTab = tk.Frame(self.tabcontrol)
        from src.notebook_tabs.uds_manual.uds_manual import Uds_Manual_Tab_Frame
        self.uds_manualTab = Uds_Manual_Tab_Frame(
            master=self, rooter=self.ROOTER)

        # can 手动操作
        # self.can_manualTab = tk.Frame(self.tabcontrol)
        from src.notebook_tabs.can_manual.can_manual import Can_Manual_Tab_Frame

        self.can_manualTab = Can_Manual_Tab_Frame(
            master=self, rooter=self.ROOTER)

        from src.notebook_tabs.ecu_lib.ecu_unitTab_Frame import Ecu_unitTab_Frame
        self.ecu_unit_Tab = Ecu_unitTab_Frame(
            master=self, rooter=self.ROOTER, config_file='config/ecu_unit_info.json')

        from src.notebook_tabs.func_lib.can_can.can_can_Frame import Can_Can_Lib_Frame
        self.can_can_Tab = Can_Can_Lib_Frame(
            master=self, rooter=self.ROOTER, config_file='config/can_can_unittest.json')

        from src.notebook_tabs.func_lib.uds_uds.uds_uds_Frame import Uds_Uds_Lib_Frame
        self.uds_uds_Tab = Uds_Uds_Lib_Frame(
            master=self, rooter=self.ROOTER, config_file='config/uds_uds_unittest.json')

        from src.notebook_tabs.func_lib.uds_can.uds_can_Frame import Uds_Can_Lib_Frame
        self.uds_can_Tab = Uds_Can_Lib_Frame(
            master=self, rooter=self.ROOTER, config_file='config/uds_can_unittest.json')

        from src.notebook_tabs.config_lib.config_lib import Config_show_lib
        self.config_lib = Config_show_lib(
            master=self.tabcontrol, rooter=self.ROOTER)

        # 发送uds报文，捕获应用报文
        # self.batc_uds_can_unitTab = Batc_Uds_Can_Unittest_Tab_Frame(
        #     master=self.tabcontrol, masterUI=self.masterUI, parentWin=self
        # )

        self.tabcontrol.add(self.uds_manualTab, text="uds手动操作")
        self.tabcontrol.add(self.can_manualTab, text="can手动操作")
        self.tabcontrol.add(self.ecu_unit_Tab, text="ECU诊断库")
        self.tabcontrol.add(self.can_can_Tab, text="can_can测试库")
        self.tabcontrol.add(self.uds_uds_Tab, text="uds_uds测试库")
        self.tabcontrol.add(self.uds_can_Tab, text="uds_can测试库")
        self.tabcontrol.add(self.config_lib, text="配置库")
        # self.tabcontrol.add(self.can_batc_unitTab, text="can_Batc单元测试库")
        # self.tabcontrol.add(self.batc_uds_can_unitTab,
        # text="Batc_发送uds返回can_单元测试库")
